//
//  XNUseCarViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/14.
//

#import "XNUseCarViewController.h"
#import "XNUseCarBillInfoModel.h"
#import "XNUseCarBillInfoRequest.h"
#import "XNUseCarApproveRequest.h"
#import "XNCarInfoRequest.h"

@interface XNUseCarViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNUseCarBillInfoModel *infoModel;

@property (nonatomic, copy) NSArray *carInfoArray;

@property (nonatomic, assign) BOOL confirmState;

@property (nonatomic, assign) BOOL sendCarState;

@property (nonatomic, assign) BOOL ApproveState;

@end

@implementation XNUseCarViewController


- (void)viewDidLoad {
    [self viewDidLoadWithoutBottomBtn];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNUseCarBillInfoRequest *infoRequest = [[XNUseCarBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNUseCarBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNUseCarBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNUseCarApproveRequest *approveRequest = [[XNUseCarApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNUseCarApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNUseCarApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNCarInfoRequest *carInfoRequest = [[XNCarInfoRequest alloc] init];
    [carInfoRequest startWithCompletionBlockWithSuccess:^(__kindof XNCarInfoRequest * _Nonnull request) {
        weakSelf.carInfoArray = request.carInfoArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNCarInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        
        NSString *account = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
        [weakSelf.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if([obj.checkAccount isEqualToString:account] && [obj.status isEqualToString:@"CS020"]){
                if([weakSelf.infoModel.createUser isEqualToString:obj.checkAccount]){
                    self.confirmState = YES;
                }else if (obj.seq == 1){
                    self.sendCarState = YES;
                }else{
                    self.ApproveState = YES;
                }
            }
        }];
        
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf customBottomBtn];
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}

- (void)customBottomBtn
{
    if (self.ApproveState) {
        [self creatBottomBtn];
    }else if (self.sendCarState) {
        [self creatSendCarBottomBtn];
    }else if(self.confirmState){
        [self creatConfirmBottomBtn];
    }
}

- (void)creatSendCarBottomBtn
{
    
    UIButton *applyBtn = [[UIButton alloc] init];
    applyBtn.backgroundColor = [UIColor colorWithHexString:@"#58BA2C"];
    [applyBtn setTitle:@"派车" forState:UIControlStateNormal];
    [applyBtn addTarget:self action:@selector(apply:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:applyBtn];
    [applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.height.mas_equalTo(is_iPhoneXSerious ? 80 : 50);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(SCREENW/2);
    }];
    
    
    UIButton *completeDismissBtn = [[UIButton alloc] init];
    completeDismissBtn.backgroundColor = [UIColor colorWithHexString:@"#EF5459"];
    [completeDismissBtn setTitle:@"驳回" forState:UIControlStateNormal];
    [completeDismissBtn addTarget:self action:@selector(completeDismiss:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:completeDismissBtn];
    [completeDismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.height.mas_equalTo(is_iPhoneXSerious ? 80 : 50);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(SCREENW/2);
    }];
}

- (void)creatConfirmBottomBtn
{
    UIButton *applyBtn = [[UIButton alloc] init];
    applyBtn.backgroundColor = [UIColor colorWithHexString:@"#58BA2C"];
    [applyBtn setTitle:@"确认" forState:UIControlStateNormal];
    [applyBtn addTarget:self action:@selector(apply:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:applyBtn];
    [applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.height.mas_equalTo(is_iPhoneXSerious ? 80 : 50);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(SCREENW);
    }];
}


- (void)apply:(UIButton *)sender
{
    self.popView = [[XNApprovePopView alloc] init];
    self.popView.showPicker = self.sendCarState ? YES : NO;
    self.popView.delegate = self;
    self.popView.dataArray = self.carInfoArray;
    self.popView.approveType = XNApproveType_Pass;
    [self.popView show];
}

- (void)completeDismiss:(UIButton *)sender
{
    self.popView = [[XNApprovePopView alloc] init];
    self.popView.showPicker = NO;
    self.popView.delegate = self;
    self.popView.dataArray = self.carInfoArray;
    self.popView.approveType = XNApproveType_CompleteDismiss;
    [self.popView show];
}

- (void)creatTableData
{
    
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeString],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.makeOrderUser],
                              [NSString stringWithFormat:@"制单部门： %@",self.infoModel.departmentName],
                              [NSString stringWithFormat:@"职务： %@",self.infoModel.makePosition],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"申请信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请人: %@",self.infoModel.proposerName],
                               [NSString stringWithFormat:@"申请部门： %@",self.infoModel.departmentName],
                               [NSString stringWithFormat:@"出发时间： %@",self.infoModel.carStartDateString],
                               [NSString stringWithFormat:@"返回时间： %@",self.infoModel.carEndDateString],
                               [NSString stringWithFormat:@"出发地： %@",self.infoModel.outAddress],
                               [NSString stringWithFormat:@"目的地： %@",self.infoModel.destination],
//                              [NSString stringWithFormat:@"用车日期： %@",self.infoModel.useCarDateString],
                               [NSString stringWithFormat:@"用车人数： %ld",self.infoModel.carNumber],
                               [NSString stringWithFormat:@"补单： %@",self.infoModel.isRepairOrder ? @"是":@"否"],
                               [NSString stringWithFormat:@"用车事由： %@",self.infoModel.useCarReasons],
                               [NSString stringWithFormat:@"用车类型： %@",self.infoModel.useCarType.length > 0 ? self.infoModel.useCarType : @""],
                               [NSString stringWithFormat:@"司机姓名： %@",self.infoModel.driverName.length > 0 ? self.infoModel.driverName : @""],
                               [NSString stringWithFormat:@"车牌号： %@",self.infoModel.carNo.length > 0 ? self.infoModel.carNo : @""],
                               [NSString stringWithFormat:@"司机电话： %@",self.infoModel.driverPhone.length > 0 ? self.infoModel.driverPhone : @""],
                               [NSString stringWithFormat:@"是否出市： %@",self.infoModel.isOutCity ? @"是":@"否"],
                               nil];
    itemModel2.showItemArray = showItemArray2;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}

@end
